using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_HeroFateTrajectoryTipAllAttribute : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_HeroFateTrajectoryTipAllAttribute
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExButton buttonCloseTips { protected set; get; }
            public UnityEngine.GameObject attributeMould { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonCloseTips", out var __tbv0);
                this.buttonCloseTips = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("attributeMould", out var __tbv1);
                this.attributeMould = __tbv1;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields
        private ListScrollAdapter<UIN_CommonAttribute> attrs = null;

        private Dictionary<int, int> dicAttrCid2Value = DictionaryPool<int, int>.Get();
        private List<int> listAttrCid = ListPool<int>.Get();
        #endregion fields

        #region properties

        protected UIB_UIN_HeroFateTrajectoryTipAllAttribute ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_HeroFateTrajectoryTipAllAttribute();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.attrs = this.InitListScroll<UIN_CommonAttribute>(this.ui.attributeMould, this.OnAttributeChanged);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            this.ui.buttonCloseTips.onClick.AddListener(this.OnClickBtnClose);
        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            if (this.dicAttrCid2Value != null)
            {
                DictionaryPool<int, int>.Put(this.dicAttrCid2Value);
                this.dicAttrCid2Value = null;
            }

            if (this.listAttrCid != null)
            {
                ListPool<int>.Put(this.listAttrCid);
                this.listAttrCid = null;
            }
        }

        #region methonds

        public void Show(HeroData hero)
        {
            this.SetData(hero);
            this.Show();

            this.ShowAttribute();
        }

        private void SetData(HeroData hero)
        {
            this.listAttrCid.Clear();
            this.dicAttrCid2Value.Clear();
            for (int i = 1; i <= hero.fateTrajectoryStage; i++)
            {
                var cfg = TableCenter.heroFateTrajectory.GetCfg(hero.cid, i);
                this.InitShowAttrInDictionary(cfg);
            }
            this.listAttrCid.AddRange(this.dicAttrCid2Value.Keys);
        }

        private void InitShowAttrInDictionary(CfgHeroFateTrajectory cfg)
        {
            if (cfg == null)
                return;
            for (var i = 0; i < cfg.AttType.Count; i++)
            {
                int cid = cfg.AttType[i];
                int val = cfg.AttValue[i];

                this.dicAttrCid2Value.TryGetValue(cid, out var count);
                count += val;
                this.dicAttrCid2Value[cid] = count;
            }
        }

        private void ShowAttribute()
        {
            int cnt = this.listAttrCid.Count;
            this.attrs.RefillCells(cnt);
        }

        private void OnAttributeChanged(UIN_CommonAttribute item, int index)
        {
            int attrCid = this.listAttrCid[index];
            int attrVal = this.dicAttrCid2Value[attrCid];
            item.ShowWhite(attrCid, attrVal);
        }

        private void OnClickBtnClose()
        {
            this.Hide();
        }
        #endregion methonds
    }
}
